package com.hdoit.pisces.web.rel.controllers;

import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.activiti.engine.impl.cmd.GetUserPictureCmd;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.hdoit.pisces.logical.rel.model.Relationship;
import com.hdoit.pisces.logical.rel.model.Relative;
import com.hdoit.pisces.logical.rel.service.IRelationshipService;
import com.hdoit.pisces.logical.rel.service.IRelativeService;
import com.hdoit.pisces.logical.viewmodel.RelationshipSearch;
import com.tennetcn.common.enums.ManageContainer;
import com.tennetcn.common.enums.ModelStatus;
import com.tennetcn.common.enums.pub.SexStatus;
import com.tennetcn.common.logical.superbase.model.Employee;
import com.tennetcn.common.logical.superbase.model.RoleEmployee;
import com.tennetcn.common.logical.superbase.service.IEmployeeService;
import com.tennetcn.common.logical.superbase.service.IRoleEmployeeService;
import com.tennetcn.common.logical.superbase.service.IRoleService;
import com.tennetcn.common.logical.viewmodel.superbase.EmployeeSearch;
import com.tennetcn.common.message.PagerModel;
import com.tennetcn.common.utils.ChineseUtils;
import com.tennetcn.common.utils.CommonUtils;
import com.tennetcn.common.utils.DateUtils;
import com.tennetcn.component.web.ActionResult;
import com.tennetcn.component.web.BaseController;
import com.tennetcn.component.web.ErrorResult;
import com.tennetcn.component.web.SessionWebFactory;


@Controller
@RequestMapping(value="/admin/rel/relationship")
public class RelationshipController extends BaseController{

	@Override
	public String getRoutePath() {
		return "areas/admin/rel/relationship/";
	}
	
	private IRelationshipService relationshipService;
	
	public IRelationshipService getRelationshipService(){
		return relationshipService;
	}
	
	@Resource
	public void setRelationshipService(IRelationshipService relationshipService){
		this.relationshipService=relationshipService;
	}
	
	private IRelativeService relativeService;
	
	public IRelativeService getRelativeService(){
		return relativeService;
	}
	
	@Resource
	public void setRelativeService(IRelativeService relativeService){
		this.relativeService = relativeService;
	}
	
	
	private IRoleEmployeeService roleEmployeeService;
	
	public IRoleEmployeeService getRoleEmployeeService(){
		return roleEmployeeService;
	}
	
	@Resource
	public void setRoleEmployeeService(IRoleEmployeeService roleEmployeeService){
		this.roleEmployeeService = roleEmployeeService;
	}
	
	private IRoleService roleService;
	
	public IRoleService getRoleService(){
		return roleService;
	}
	
	@Resource
	public void setRoleService(IRoleService roleService){
		this.roleService = roleService;
	}
	
	private IEmployeeService employeeService;
	
	public IEmployeeService getEmployeeService(){
		return employeeService;
	}
	
	@Resource
	public void setEmployeeService(IEmployeeService employeeService){
		this.employeeService = employeeService;
	}
	
	
	private final String indexViewName="index";
	private final String outputViewName="output";
	private final String detailedViewName="relationshipDetailed";
	private final String saveViewName="save";
	private final String bsFileUploadViewName="bsFileUpload";
	
	@RequestMapping(value=indexViewName,method={RequestMethod.GET,RequestMethod.POST})
	public String index(Model model,RelationshipSearch relationshipSearch,EmployeeSearch search,PagerModel pagerModel,boolean ajaxPost){
		//System.out.println("111111");
		String key = getLoginModel().getLoginId();
		//System.out.println(getLoginModel().getLoginId());
		String userKey = roleService.queryModelByMark("3").getRoleId();//普通用户的roleid
		//System.out.println(userKey);
		String managerKey = roleService.queryModelByMark("2").getRoleId();//普通管理员的roleid
		//roleService.queryModel(userKey)
		System.out.println(managerKey);
		List<Relationship> relationships=relationshipService.queryList(relationshipSearch);
		
		List<RoleEmployee>role_employees=roleEmployeeService.queryList(search);
		
	//	Relationship relationship = new Relationship();
		
		if(roleEmployeeService.queryRoleIdsByEmployeeId(key).equals(userKey)){
			//普通用户
			for(int i = relationships.size()-1; i >=0;i--){
				if(!relationships.get(i).getEmployeeId().equals(key)){
					relationships.remove(i);
				}
			}
			pagerModel.setTotalAmount(relationships.size());
			
		}else if(roleEmployeeService.queryRoleIdsByEmployeeId(key).equals(managerKey)){
			//普通管理员
			for(int i = relationships.size()-1; i >=0;i--){
				if(	!roleService.queryModel(roleEmployeeService
											.queryModel(relationships.get(i).getEmployeeId())
											.getRoleId()).getRoleMark().equals("2")){
					relationships.remove(i);
				}
			}
			pagerModel.setTotalAmount(relationships.size());
			
		}else{
			//超级管理员
		//	List<Relationship> relationships=relationshipService.queryList(relationshipSearch);
			pagerModel.setTotalAmount(relationshipService.queryCount(relationshipSearch));
		}
	
	
		
	//	model.addAttribute("employees", employees);
		model.addAttribute("employeeSearch",search);
		
		//model.addAttribute("sessionWebFactory", sessionWebFactory);
		
		model.addAttribute("role_employees", role_employees);
		
		model.addAttribute("relationships",relationships);
		model.addAttribute("relationshipSearch",relationshipSearch);
		model.addAttribute(ManageContainer.PAGERKEY,pagerModel);
		
		if(ajaxPost){
			return view(outputViewName);
		}
		return view(indexViewName);
	}
	
	@RequestMapping(value=detailedViewName,method={RequestMethod.GET,RequestMethod.POST})
	public String relationshipDetailed(Model model,String key){
		Relationship relationship=new Relationship();
		//relationship.setRelationship_sex(SexStatus.male);
		
		if(!StringUtils.isEmpty(key)){
			relationship=relationshipService.queryModel(key);
		}
		model.addAttribute("relationship",relationship);
		return view(detailedViewName);
	}
	
	@RequestMapping(value=saveViewName,method={RequestMethod.GET,RequestMethod.POST})
	public String save(Model model,Relationship relationship,HttpServletRequest request){
		
		Relative relative = new Relative();
	//	Employee employee = new  Employee();
		if(StringUtils.isEmpty(relationship.getRelationshipId())){
			relationship.setRelationshipId(CommonUtils.getKeyId());
			relative.setRelativeId(CommonUtils.getKeyId());
			String key = relative.getRelativeId();
			System.out.println(key+"8989898998989898");
			relationship.setRelativeId(key);
			relationship.setEmployeeName(relationship.getEmployeeName());
			System.out.println(relationship.getEmployeeName()+"111111111111111111111111111");
			System.out.println(employeeService.queryListByRoleMark("3").size()+"33333333333333333333333333");
			
			List<Employee> employees=employeeService.queryListByRoleMark("3");
			
			for(int i =employees.size()-1;i>=0;i-- ){
				if(!employees.get(i).getEmployeeName().equals(relationship.getEmployeeName()))
					employees.remove(i);
			}
			System.out.println(employees.size()+"2121212121121");
		//	System.out.println(employeeService.queryModel(relationship.getEmployeeName()).getEmployeeId()+"222222222222222222222222");
			
			//System.out.println(employees.get(0).getEmployeeId());
			relationship.setEmployeeId(employees.get(0).getEmployeeId());
			System.out.println(relationship.getRelative_firstName()+"454545454545454445");
			relationship.setRelative_firstName(relationship.getRelative_firstName());
			relationship.setRelative_lastName(relationship.getRelative_lastName());
			relative.setRelative_firstName(relationship.getRelative_firstName());
			relative.setRelative_lastName(relationship.getRelative_lastName());
		
			//relative.setCreateDate(DateUtils.getCurrent());
//			relative.setCreateUserId(getLoginModel().getLoginId());
//			relative.setCreateUserName(getLoginModel().getLoginName());
			relationship.setModelStatus(ModelStatus.add);
			relative.setModelStatus(ModelStatus.add);
			
		}else{
//			student.setModifyDate(DateUtils.getCurrent());
//			student.setModifyUserId(getLoginModel().getLoginId());
//			student.setModifyUserName(getLoginModel().getLoginName());
			relationship.setModelStatus(ModelStatus.update);
		}
//		relative.setMarkCode(ChineseUtils.getPinYinHeadChar(relative.getRelative_name()));
//		relative.setCode(ChineseUtils.getPingYin(relative.getRelative_name()));
		
		ActionResult actionResult;
        if(relationshipService.applyChange(relationship)){
            actionResult=new ActionResult();
        }else{
            actionResult=new ErrorResult();
        }
        if(relativeService.applyChange(relative)){
            actionResult=new ActionResult();
        }else{
            actionResult=new ErrorResult();
        }

        model.addAttribute("relationship", relationship);
        model.addAttribute("relative",relative);
        model.addAttribute(ManageContainer.RESULTKEY, actionResult);
		
		return view(detailedViewName); 
	}
	
	@RequestMapping(value = bsFileUploadViewName, method = { RequestMethod.GET })
	public String bsFileUpload(Model model){
		return view(bsFileUploadViewName);
	}
	

}
